package com.obtk.shilipai.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.obtk.shilipai.entity.Dishe;
import com.obtk.shilipai.entity.Order;
import com.obtk.shilipai.entity.Shop;
import com.obtk.shilipai.entity.ShopMenu;
import com.obtk.shilipai.service.OrderService;
import com.obtk.shilipai.service.ShopMenuService;
import com.obtk.shilipai.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

@Controller
public class ShopMenuController {
    @Autowired
    private ShopMenuService shopMenuService;
    @Autowired
    private OrderService orderService;

    @RequestMapping("/getShopMenu")
    public String getShopMenu(Model model, HttpServletRequest request,
                             HttpServletResponse response, HttpSession session,@RequestParam(required = false, defaultValue = "1") int pageNo,
                              @RequestParam(required = false, defaultValue = "10") int pageSize) {
        Shop user = (Shop) session.getAttribute("user");
        List<ShopMenu> menu = shopMenuService.getShopMenu();
        BigDecimal orderMoney = shopMenuService.getOrderMoney(user.getShopId());
        int orderNum = shopMenuService.getOrderNum(user.getShopId());
        int appNum = shopMenuService.getAppNum(user.getShopId());
        String appAvg = shopMenuService.getAppAvg(user.getShopId());
        model.addAttribute("menu", menu);
        if (orderMoney!=null){
            session.setAttribute("orderMoney", orderMoney);
        }else {
            session.setAttribute("orderMoney", null);
        }
        if (orderNum!=0){
            session.setAttribute("orderNum", orderNum);

        }else {
            session.setAttribute("orderNum", null);
        }
        if (appNum!=0){
            session.setAttribute("appNum", appNum);

        }else {
            session.setAttribute("appNum", null);
        }
        if (appAvg!=null){
            session.setAttribute("appAvg", new Double(appAvg));
        }else {
            session.setAttribute("appAvg", null);
        }
        PageHelper.startPage(pageNo, pageSize);
        List<Order> order = orderService.getOrderByAll(user.getShopId());
        if (order.size()!=0){
            session.setAttribute("order", order);
        }else {
            session.setAttribute("order", null);
        }

        return "main";
    }
}
